/*!
 * Copyright 2006 Nelson Daza. nelson.daza@gmail.com. All rights reserved.
 * 
 * functions.js
 * version 1.0.0.0 major.minor[.revision[.build]]
 */
	
 /* START Pop-up Windows Script */

	function popup ( url, name, width, height, posx, posy, features )	{
		var func = null;
		if ( typeof( posx ) == 'undefined' )
			posx = 'center';
		if ( typeof( posy ) == 'undefined' )
			posy = 'middle';
		if ( typeof( features ) == 'undefined' )
			features = '';

		if ( posx.toLowerCase ( ) == 'center' )
			posx = ( screen.width - width ) / 2;
		else if ( posx.toLowerCase ( ) == 'right' )
			posx = (screen.width - width - 30);
		else if ( posx < 0 )
			posx = screen.width - width + posx;
		else 
			posx = 0;
	
		if ( posy.toLowerCase ( ) == 'middle' )
			posy = ( screen.height - height ) / 2;
		else if ( posy.toLowerCase ( ) == 'bottom' )
			posy = ( screen.height - height - 60 );
		else if ( posy < 0 )
			posx = screen.height - height - posy - 30;
		else 
			posy = 0;
	
		func = window.open;
		if( features.indexOf( "dependent=yes" ) >= 0 && window.showModalDialog )
			func = window.showModalDialog;
		
		if ( typeof( name ) == 'undefined' || typeof( width ) == 'undefined' || typeof( height ) == 'undefined' )
			return func( url );
		else
			return func( url, name, 'dialogHeight:' + height + 'px; dialogWidth:' + width + 'px; dialogLeft:' + posx + 'px; dialogTop:' + posy + 'px;,' + 'width=' + width + ',height=' + height + ',screenX=' + posx + ',left=' + posx + ',screenY=' + posy + ',top=' + posy + ',' + features );
	}

	function showSWF( filePath, id, width, height, bgcolor, target, flashvars, align, quality, wmode, allowScriptAccess )	{
		var sHTML = '';
		
		if( !bgcolor )
			bgcolor = 'transparent';
		if( !align )
			align = 'center';
		if( !quality )
			quality = 'high';
		if( !flashvars )
			flashvars = '';
		if( !wmode )
			wmode = 'transparent';
		if( !allowScriptAccess )
			allowScriptAccess = 'sameDomain';

		if ( target )	{
			target = toObject ( target );
			if( !target )	{
				alert( 'SWF Target Error' );
				return;
			}
		}
		
		sHTML = '' +
			'<div style="background-color:' + bgcolor + ';';
		if( width )
			sHTML += 'width:' + width + 'px;';
		if( height )
			sHTML += 'height:' + height + 'px;';
		sHTML += '">' +
			'	<object type="application/x-shockwave-flash" ' + 
			'		data="' + filePath + '" ';
		if( width )
			sHTML += '		width="' + width + '" ';
		if( height )
			sHTML += '		height="' + height + '" ';
		sHTML += '' +
			'		id="' + id + '" ' + 
			'		align="' + align + '">' + 
			'			<param name="flashvars" value="' + flashvars + '">' + 
			'			<param name="allowScriptAccess" value="' + allowScriptAccess + '" />' + 
			'			<param name="movie" value="' + filePath + '" />' + 
			'			<param name="quality" value="' + quality + '" />' + 
			'			<param name="bgcolor" value="' + bgcolor + '" />' + 
			'			<param name="wmode" value="' + wmode + '">' + 
			'	</object>' +
			'</div>';

		if ( target )
			target.innerHTML = sHTML;
		else
			document.write( sHTML );
	}
	
	function trace( text, clear )	{
		var elem = document.getElementById( 'trace_element' );
		if( !elem )	{
			elem = document.createElement( 'div' );
			elem.id = 'trace_element';
			elem.className = 'text table_border';
			elem.style.backgroundColor = '#EEEEEE';
			elem.style.padding = '5px';
			elem.style.width = '50%';
			elem.style.height = '50%';
			elem.style.position = 'absolute';
			elem.style.top = "0px";
			elem.style.left = "0px";
			elem.style.overflow = "auto";
			document.body.appendChild( elem );
		}
		elem.innerHTML = ( clear ? '' : elem.innerHTML ) + '<br />' + text;
	}

	/* END Pop-up Windows Script */
	
	function changeFont( num )	{
		if( !window.font_size )	{
			window.font_size = 9;
			window.MAX = 17;
			window.MIN = 11;
		}

		window.font_size += num;
		if( window.font_size > window.MAX )
			window.font_size = window.MAX;
		if( window.font_size < window.MIN )
			window.font_size = window.MIN;
		 document.getElementsByTagName( 'body' )[0].style.fontSize = window.font_size + 'px';
	}
	
	function validateKey ( evt, key )	{
		if ( !evt )
			evt = window.event;

		if ( evt.keyCode && evt.keyCode == key )
			return true;
		else if ( evt.charCode && evt.charCode == key )
			return false;

		return false;
	}

	function objectToString ( obj, ident, maxlevel )	{
		var string = "", tab = "", c = 0;
		ident = ( ident ? ident + 1 : 0 );
		maxlevel = ( maxlevel ? maxlevel -1 : 1 );
		
		if ( maxlevel == -1 )
			return "";
		
		for ( c = 0; c < ident; c ++ )
			tab += "  ";
		string += tab + "[" + typeof(obj) + "]" + obj;
		
		ident ++;
		tab += "  ";
		
		for ( subObj in obj )	{
			string += "\n" + tab + "[" + subObj + "] => " + obj[subObj];
			if( typeof( obj[subObj] ) == 'object' && obj[subObj] && confirm ( typeof(obj[subObj]) + obj[subObj] ) )
				string += "\n" + objectToString ( subObj, ident, maxlevel );
		}
		return string;
	}
	
	function toObject ( something )	{
		if ( typeof( something ) == 'string' )
			return document.getElementById ( something );
		if ( typeof( something.nodeName ) != 'undefined' )
			return something;
		return null;
	}
	
	function jumpTo ( url )	{
		document.location.href = url;
	}
	
	function hideElement ( something )	{
		something = toObject ( something );
		if ( something )	{
			something.style.visibility = 'hidden';
			something.style.display = 'none';
		}
	}
	
	function showElement ( something )	{
		something = toObject ( something );
		if ( something )	{
			something.style.visibility = 'visible';
			something.style.display = 'block';
		}
	}
	
	function changeVisibility ( something )	{
		something = toObject ( something );
		if ( something )	{
			if ( something.style.visibility == 'visible' )
				hideElement ( something );
			else
				showElement ( something );
		}
	}
	
	function setElementPosition( something, posx, posy )	{
		something = toObject( something );
		something.style.position = 'absolute';
		something.style.left = posx + 'px';
		something.style.top = posy + 'px';
	}
	
	function getElementRec ( something )	{
		something = toObject( something );
		var tmp = something, nLeft = 0, nTop = 0;
		while ( tmp && typeof( tmp.offsetLeft ) != 'undefined' && typeof( tmp.offsetTop ) != 'undefined' )	{
			nLeft += Number( tmp.offsetLeft );
			if( tmp.nodeName.toLowerCase( ) != 'td' )
				nTop += Number( tmp.offsetTop );
			tmp = tmp.parentNode;
		}
		
		return { left: nLeft, top: nTop, right: (nLeft + Number( something.offsetWidth )), bottom: (nTop + Number( something.offsetHeight )), width : something.offsetWidth, height: something.offsetHeight };
	}
	
	function setElementOpacity ( something, opacity )	{
		something = toObject( something );
		if( document.all )
			something.style.filter = 'alpha(opacity=' + ( opacity * 100 ) + ')';
		else
			something.style.MozOpacity = opacity;
	}
	
	function isDate ( year, month, day )	{
		var arr = null, tempDate = null, nyear = null;
		if ( year.toString().length == 0 )
			return false;
		if ( day == null )	{
			month = ( month == null ) ? '-' : month;
			if ( year.toString().indexOf( month ) == -1 )
				return false;
			
			arr = year.toString().split( month );
			if ( arr.length != 3 )
				return false;
			year = arr[0];
			month = arr[1];
			day = arr[2];
		}
		month = month - 1;  // javascript month range : 0- 11
		tempDate = new Date ( year, month, day );
		nyear = ( tempDate.getYear ( ) < 1000 ) ? tempDate.getYear ( ) + 1900 : tempDate.getYear ( );
		
		return ( ( nyear == year ) && ( month == tempDate.getMonth ( ) ) && ( day == tempDate.getDate ( ) ) );
	}
	
	function imagenChange ( image, source )	{
		image = toObject ( image );
		if ( image )
			image.src = source;
	}
	
	function checkMaxChars( something, maxChars ){
		something = toObject ( something );
		if ( something && something.value.length > maxChars )
			something.value = something.value.substring( 0, maxChars );
	}
	
	function innerChange ( something, child )	{
		something = toObject ( something );
		if ( something )	{
			while ( something.hasChildNodes( ) )
				something.removeChild( something.firstChild );
			if ( typeof ( text ) == 'string' )
				something.appendChild( document.createTextNode( child ) );
			else
				something.appendChild( child );
		}
	}

	function moveElement ( elem, nX, nY, nXStepIn, nYStepIn, bPercent, nInterval, onCallBack )	{
		elem = toObject( elem );
		var style = elem.style, nDirX = 0, nDirY = 0, nXStep = nXStepIn, nYStep = nYStepIn, left = 0, top = 0, deficit = 0, func = null;
		
		if ( nXStep <= 0 )
			nXStep = 1;
	
		if ( nYStep <= 0 )
			nYStep = 1;
			
		left = Number ( style.left.replace( 'px', '' ) );
		top = Number ( style.top.replace( 'px', '' ) );
		
		deficit = Math.abs( nX - left );
		if ( left != nX && deficit > 0 )	{
			if ( bPercent )
				nXStep = Math.ceil ( nXStep * deficit / 100 ) + 2;
			
			if ( deficit < nXStep )
				nXStep = deficit;
			
			nDirX = ( nX - left >= 0 ? 1 : -1 );
			
			left += ( nXStep * nDirX );
			style.left = left + 'px';
		}

		deficit = Math.abs( nY - top );
		if ( top != nY && deficit > 0 )	{
			if ( bPercent )
				nYStep = Math.ceil ( nYStep * deficit / 100 ) + 2;

			if ( deficit < nYStep )
				nYStep = deficit;
			
			nDirY = ( nY - top >= 0 ? 1 : -1 );
			
			top += ( nYStep * nDirY );
			style.top = top + 'px';
		}
		
		if ( nDirX == 0 && nDirY == 0 )	{
			if ( typeof( onCallBack ) != 'undefined' )	{
				func = new Function( onCallBack );
				func( );
			}
		}
		else
			elem.timeout = setTimeout( "moveElement ( '" + elem.id + "', " + nX + ", " + nY + ", " + nXStepIn + ", " + nYStepIn + ", " + bPercent + ", " + nInterval + ", \"" + onCallBack + "\" )", nInterval );
	}

	function resizeElement ( elem, nW, nH, nWStepIn, nHStepIn, bPercent, nInterval, onCallBack )	{
		elem = toObject( elem );
		var style = elem.style, nDirW = 0, nDirH = 0, nWStep = nWStepIn, nHStep = nHStepIn, width = 0, height = 0, func = null;
		
		if ( nWStep <= 0 )
			nWStep = 1;
	
		if ( nHStep <= 0 )
			nHStep = 1;
			
		width = ( style.width ? style.width : elem.width );
		height = ( style.height ? style.height : elem.height );
		
		width = ( width ? Number ( width.replace( 'px', '' ).replace( '%', '' ) ) : 0 );
		height = ( height ? Number ( height.replace( 'px', '' ).replace( '%', '' ) ) : 0 );
		
		if ( Math.abs( nW - width ) > 0 )	{
			if ( bPercent )
				nWStep = Math.ceil ( ( nWStepIn ) * Math.abs ( nW - width ) / 100 );
			
			if ( Math.abs( nW - width ) < Math.abs( nWStep ) + 1 )
				nWStep = Math.abs( nW - width );
			
			nDirW = ( nW - width ) >= 0 ? 1 : -1;
			
			width += nWStep * nDirW;
			style.width = width + 'px';
		}

		if ( Math.abs( nH - height ) > 0 )	{
			if ( bPercent )
				nHStep = Math.ceil ( ( nHStep ) * Math.abs( nH - height ) / 100 );

			if ( Math.abs( nH - height ) < Math.abs( nHStep ) + 1 )
				nHStep = Math.abs( nH - height );
			
			nDirH = ( nH - height ) >= 0 ? 1 : -1;
			
			height += nHStep * nDirH;
			style.height = height + 'px';
		}
		
		if ( nDirW == 0 && nDirH == 0 )	{
			if ( typeof( onCallBack ) != 'undefined' )	{
				func = new Function( onCallBack );
				func( );
			}
		}
		else
			elem.timeout = setTimeout( "resizeElement ( '" + elem.id + "', " + nW + ", " + nH + ", " + nWStepIn + ", " + nHStepIn + ", " + bPercent + ", " + nInterval + ", \"" + onCallBack + "\" )", nInterval );
	}

	function fadeElement ( elem, nAlpha, nStep, nInterval, onCallBack )	{
		elem = toObject( elem );
		var style = elem.style, actualAlpha = ( style.MozOpacity ? style.MozOpacity : 1 ), nDir = 0, nTotal = 0, func = null;
		
		if( nStep <= 0 )
			nStep = 0.05;
		else if( nStep > 1 )
			nStep /= 100;
		
		if( nAlpha < 0 )
			nAlpha = 0;
		else if( nAlpha > 1 )
			nAlpha /= 100;
		
		nTotal = Math.abs( Number( nAlpha ) - Number ( actualAlpha ) );
		if ( nTotal > 0 )	{
			if ( nTotal < Math.abs( nStep ) + 0.01 )
				nStep = nTotal;
			
			nDir = ( Number( nAlpha ) - Number ( actualAlpha ) ) >= 0 ? 1 : -1;
			
			actualAlpha = ( Number( actualAlpha ) + Number( nStep * nDir ) );
			style.MozOpacity = actualAlpha;
			style.filter = 'alpha(opacity=' + parseInt( actualAlpha * 100 ) + ')';
		}

		if ( actualAlpha == nAlpha )	{
			if ( typeof( onCallBack ) != 'undefined' )	{
				func = new Function( onCallBack );
				func( );
			}
		}
		else
			elem.timeout = setTimeout( "fadeElement ( '" + elem.id + "', " + nAlpha + ", " + nStep + ", " + nInterval + ", \"" + onCallBack + "\" )", nInterval );
	}

	/* START SortTable Script */
	function sortTable ( column )	{
		var table = null, groups = null, i = 0;
		column = toObject ( column );
		if ( column )	{
			table = findParentNode ( column, 'table' );
			if ( table == null )
				return;
			groups = table.getElementsByTagName ( 'tbody' );
			for( i = 0; i < groups.length; i++ )	{
				sortTableRows ( groups[i], column.parentNode.cellIndex );
			}
		}
	}
	
	function sortTableRows ( group, col )	{
		var oRows = new Array ( ), iRows = new Array ( ), c = 0, oCol = new Array ( ), vCol = new Array ( );


		for ( c = 0; c < group.rows.length; c++ )	{
			oRows[c] = group.rows[c].cloneNode ( true );
			oRows[c].onmouseover = group.rows[c].onmouseover;
			oRows[c].onmouseout = group.rows[c].onmouseout;
			oRows[c].onclick = group.rows[c].onclick;
			oRows[c].ondblclick = group.rows[c].ondblclick;
			
			oRows[c].cells[col].onmouseover = group.rows[c].cells[col].onmouseover;
			oRows[c].cells[col].onmouseout = group.rows[c].cells[col].onmouseout;
			oRows[c].cells[col].onclick = group.rows[c].cells[col].onclick;
			oRows[c].cells[col].ondblclick = group.rows[c].cells[col].ondblclick;
			
			iRows[c] = group.rows[c].sectionRowIndex;
		}

		for ( c = 0; c < iRows.length; c++ )	{
			if ( group.rows[c].cells[col] && group.rows[c].cells[col].firstChild )	{
				if( group.rows[c].cells[col].firstChild.nodeName.toLowerCase( ) == 'img' )
					vCol[c] = oCol[c] = [group.rows[c].cells[col].firstChild.nextSibling.nodeValue,iRows[c]];
				else
					vCol[c] = oCol[c] = [group.rows[c].cells[col].firstChild.nodeValue,iRows[c]];
			}
			else
				vCol[c] = oCol[c] = ['',iRows[c]];
		}

		oCol.sort ( compareAsString );	//sorts the content array

		if ( vCol.toString ( ) == oCol.toString ( ) )
			oCol.reverse ( );	//if the content was already sorted, reverse

		for ( c = 0; c < group.rows.length; c++ )	{
			group.replaceChild ( oRows[oCol[c][1]], group.rows[c] );	//writes the rows in a sorted/reversed order
		}
	}

	/* END SortTable Script */
	function compareAsString ( obj1, obj2 )	{
		var str1 = obj1.toString( ).toLowerCase( ), str2 = obj2.toString( ).toLowerCase( );
		
		if ( str1.lastIndexOf ( ',' ) > 0  && str1.substring ( 0, str1.lastIndexOf ( ',' ) ).trim ( ) && !isNaN ( str1.substring ( 0, str1.lastIndexOf ( ',' ) ).trim ( ) ) )
			str1 = str1.substring ( 0, str1.lastIndexOf ( ',' ) ).trim ( );

		if ( str2.lastIndexOf ( ',' ) > 0  && str2.substring ( 0, str2.lastIndexOf ( ',' ) ).trim ( ) && !isNaN ( str2.substring ( 0, str2.lastIndexOf ( ',' ) ).trim ( ) ) )
			str2 = str2.substring ( 0, str2.lastIndexOf ( ',' ) ).trim ( );
		
		if ( str1 == str2 )
			return 0;

		if ( str1 && !isNaN ( str1 ) && str2 && !isNaN ( str2 ) )	{
			return Number( str1 ) - Number ( str2 );
		}
		return ( str1 > str2 ) ? 1 : -1;
	}
	
	function findParentNode ( something, parentName )	{
		var elem = toObject ( something );
		if ( elem )	{
			while ( elem.parentNode.nodeName.toLowerCase ( ) != parentName.toLowerCase ( ) && elem.parentNode.nodeName.toLowerCase ( ) != 'body' )
				elem = elem.parentNode;
	
			if ( elem.parentNode.nodeName.toLowerCase ( ) != parentName.toLowerCase ( ) )
				return null;
			else
				return elem.parentNode;
		}
		return null;
	}
	
	/* START String general purpouse */
	String.prototype.trim = function( )	{
		return this.replace( /^\s+|\s+$/g, "" );
	}
	String.prototype.ltrim = function( )	{
		return this.replace( /^\s+/, "" );
	}
	String.prototype.rtrim = function( )	{
		return this.replace( /\s+$/, "" );
	}

	function setPosition( id, parent, pos )	{
		toObject( 'file_file' ).value = id;
		toObject( 'file_parentId' ).value = parent;
		toObject( 'file_pos' ).value = pos;
		toObject( 'file_form' ).submit( );
	}
	function deleteProperty( id, name )	{
		if( confirm( unescape( 'Do you confirm the elimination of property "' + name + '"?' ) ) )	{
			toObject( 'file_file' ).value = id;
			toObject( 'file_propertyDelete' ).value = name;
			toObject( 'file_form' ).submit( );
		}
	}
	function deleteFile ( id, name )	{
		if( confirm( unescape( 'Do you confirm the elimination of file "' + name + '"?' ) ) )	{
			toObject( 'file_delete' ).value = id;
			toObject( 'file_form' ).submit( );
		}
	}
	function activateFile( id, name, active )	{
		//if( confirm( unescape( 'Do you confirm the ' + ( active ? 'activation' : 'deactivation' ) + ' of file "' + name + '"?' ) ) )	{
		{
			toObject( 'file_file' ).value = id;
			toObject( 'file_active' ).value = ( active ? '1' : '0' );
			toObject( 'file_form' ).submit( );
		}
	}
	
	function treeClick( id, force )	{
		var tree = toObject( 'tree_' + id ), img = toObject( 'img_' + id ), holder = toObject( 'holder_' + id );
		if( !tree || !img || !holder )
			return;
		
		if( !force && tree.src.indexOf( 'expanded' ) > 0 )	{
			tree.src = tree.src.replace( 'expanded', 'colapsed' );
			hideElement( holder );
			img.src = img.src.replace( '-open', '' );
		}
		else	{
			tree.src = tree.src.replace( 'colapsed', 'expanded' );
			showElement( holder );
			img.src = img.src.replace( '.png', '-open.png' );
		}
	}